﻿using Bio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Candy.Controls
{
    public partial class SequenceControl : UserControl
    {


        private void CalculateLineNumbers()
        {
           
            string seq = ToFasta(Sequence);
            if (Sequence == null || Sequence.Length < 80 )
            {
                int start = Sequence != null ? Sequence.Length : 0;
                string t = Sequence;
                for (int i = start; i < 80; i++)
                {
                    t += " ";
                }
                seqTextBox.TextChanged -= seqTextBox_TextChanged;
                seqTextBox.Text = t + "\r\n";
                seqTextBox.TextChanged += seqTextBox_TextChanged;
            }
            else
            {
                seqTextBox.TextChanged -= seqTextBox_TextChanged;
                seqTextBox.Text = seq;
                seqTextBox.TextChanged += seqTextBox_TextChanged;
            }
            leftLineNumbers.Text = "";
            rightLineNumbers.Text = "";
            if (string.IsNullOrEmpty(Sequence))
                return;
            int numberOfLines = Sequence.Length / 80;
            int rest = Sequence.Length % 80;

            for (int i = 0; i < numberOfLines; i++)
            {
                leftLineNumbers.Text += ((i * 80) + 1).ToString("0000") + "\r\n";
                rightLineNumbers.Text += ((i * 80) + 80).ToString("0000") + "\r\n";
            }
            if (rest != 0 || numberOfLines == 0)
            {
                leftLineNumbers.Text += ((numberOfLines * 80) + 1).ToString("0000");
                rightLineNumbers.Text += ((numberOfLines * 80) + rest).ToString("0000");
            }

        }

        /// <summary>
        /// The <see cref="Sequence" /> dependency property's name.
        /// </summary>
        public const string SequencePropertyName = "Sequence";

        /// <summary>
        /// Gets or sets the value of the <see cref="Sequence" />
        /// property. This is a dependency property.
        /// </summary>
        public string Sequence
        {
            get
            {
                return (string)GetValue(SequenceProperty);
            }
            set
            {
                SetValue(SequenceProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="Sequence" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty SequenceProperty = DependencyProperty.Register(
            SequencePropertyName,
            typeof(string),
            typeof(SequenceControl),
            new PropertyMetadata(null, OnSequenceChanged));

        private static void OnSequenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //   seq  e.NewValue
            SequenceControl sc = (SequenceControl)d;
            sc.CalculateLineNumbers();

        }


        public SequenceControl()
        {
            InitializeComponent();
            
            seqTextBox.TextChanged += seqTextBox_TextChanged;
            this.Loaded += SequenceControl_Loaded;
        }

        void SequenceControl_Loaded(object sender, RoutedEventArgs e)
        {
            CalculateLineNumbers();
            this.MinWidth = this.ActualWidth;
        }

        void seqTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Sequence = ToString(seqTextBox.Text);
        }

        private string ToFasta(string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                string fake = "";
                for (int i = 0; i < 80; i++)
                {
                    fake += " ";
                }
                return fake;
            }
            ISequence s = new Bio.Sequence(Bio.Alphabets.AmbiguousProtein, text);
            Bio.IO.FastA.FastAFormatter formatter = new Bio.IO.FastA.FastAFormatter();
            string ret = formatter.Format(s, true);
            return ret;

        }

        private string ToString(string text)
        {
            if (String.IsNullOrEmpty(text))
                return "";
            return text.Replace("\r", "").Replace("\n", "").Replace(" ", "");
        }
    }
}
